package main

import (
	"fmt"
	"log"
	"os"
)

// return minimum integer number
func min(a int, b int, c int) int {
	if a <= b && a <= c {
		return a
	} else {
		if b <= a && b <= c {
			return b
		} else {
			return c
		}
	}
}

func main() {
	// This program is a LD algorithm example.

	var runeA, runeB []byte

	if len(os.Args) < 3 {
		log.Println("This is a LD algorithm program use to compare strings.\n" +
			"Usage:\n" +
			"\tgo4ld <Arg String1> <Arg String2>\n" +
			"Example:\n" +
			"\tString1:hello,tim\n" +
			"\tString2:Hell0,Jimmy")
		runeA = []byte("hello,tim")
		runeB = []byte("Hell0,Jimmy")
	} else {
		runeA = []byte(os.Args[1])
		runeB = []byte(os.Args[2])
	}

	colNum := len(runeA)
	rowNum := len(runeB)

	// define a Two-dimensional array to save result
	var arrayLD = make([][]int, rowNum+1)

	//添加首行
	var rowA = make([]int, 0)
	for i := 0; i <= colNum; i++ {
		rowA = append(rowA, i)
	}
	arrayLD[0] = rowA

	//添加其它行,首列是行数，总行数为colNum+1
	for i := 1; i <= rowNum; i++ {
		arrayLD[i] = make([]int, colNum+1)
		arrayLD[i][0] = i
	}

	//填充比较结果
	for i, v := range runeB {
		for n, w := range runeA {
			if v == w {
				arrayLD[i+1][n+1] = arrayLD[i][n]
			} else {
				arrayLD[i+1][n+1] = min(arrayLD[i][n], arrayLD[i+1][n], arrayLD[i][n+1]) + 1
			}
		}
	}

	// 打印数组结果
	for i := 0; i < len(arrayLD); i++ {
		fmt.Println(arrayLD[i])
	}

	// 打印LD(A, B)
	fmt.Println("LD(A,B)=", arrayLD[rowNum][colNum])
}

/*
   LD算法 - Python实现
	//for i, v := range os.Args[1] {
	//	for n, w := range os.Args[2] {
	//		if v == w {
	//			hashtable[i+1][n+1] = hashtable[i][n]
	//		} else {
	//			hashtable[i+1][n+1] = min(hashtable[i][n],hashtable[i+1][n], hashtable[i][n+1]) + 1
	//		}
	//
	//	}
	//}
*/
